﻿using Lottery.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LINQtoCSV;

namespace Lottery.Db
{
    public class NationalLotteryUkraineCsvRepository : IReadOnlyRepository
    {
        public String Path { get; private set; }

        public NationalLotteryUkraineCsvRepository(String path)
        {
            this.Path = path;
        }

        public IGameHistory GetGameHistory()
        {
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar = ';',
                FirstLineHasColumnNames = true
            };

            CsvContext cc = new CsvContext();

            var lotteryDescription = new LotteryDescription() { Name = "NationalLotteryUkraine", NumbersCount = 6, StartNumber = 1 };

            IEnumerable<NationalLotteryUkraine> gameNumbers = cc.Read<NationalLotteryUkraine>(this.Path, inputFileDescription);

            var games = gameNumbers.Select(gn => new Game() 
            {
                Date = gn.Date, 
                LotteryDescription = lotteryDescription, 
                NumberSet = new Combination() 
                { 
                    Numbers = new List<int>() { gn.Ball1, gn.Ball2, gn.Ball3, gn.Ball4, gn.Ball5, gn.Ball6 }
                }
            }).OrderByDescending(g => g.Date);

            return new GameHistory() { History = games };
        }
    }
}
